package com.lanling.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanling.eduservice.entity.EduSubject;
import com.lanling.eduservice.entity.excel.SubjectData;
import com.lanling.eduservice.entity.subject.OneSubject;
import com.lanling.eduservice.entity.subject.TwoSubject;
import com.lanling.eduservice.listener.SubjectExcelListener;
import com.lanling.eduservice.mapper.EduSubjectMapper;
import com.lanling.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 胡振华
 * @since 2020-11-09
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile multipartFile,EduSubjectService eduSubjectService) {
        try {
            InputStream in = multipartFile.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getOneTwoSubjects() {
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        List<OneSubject> finalSubjectList = new ArrayList<>();

        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            List<TwoSubject> twoSubjects = new ArrayList<>();
            int count = 0;
            for (int i1 = 0; i1 < twoSubjectList.size(); i1++) {
                EduSubject eduSubject1 = twoSubjectList.get(i1);

                if (eduSubject1.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject1,twoSubject);
                    twoSubjects.add(twoSubject);
                    count++;
                }
            }
            oneSubject.setCount(count);
            oneSubject.setChildren(twoSubjects);
        }
        return finalSubjectList;
    }

    //新增一级分类名称
    @Override
    public boolean addOneSubject(String title) {
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("title",title);
        EduSubject eduSubject = baseMapper.selectOne(wrapperOne);
        if (eduSubject == null){
            eduSubject = new EduSubject();
            eduSubject.setTitle(title);
            eduSubject.setParentId("0");
            int insert = baseMapper.insert(eduSubject);
            if (insert != 0){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    //新增二级分类
    @Override
    public boolean addTwoSubject(String subjectParentId,String title) {
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("title",title);
        EduSubject eduSubject = baseMapper.selectOne(wrapperOne);
        if (eduSubject == null){
            eduSubject = new EduSubject();
            eduSubject.setParentId(subjectParentId);
            eduSubject.setTitle(title);
            int insert = baseMapper.insert(eduSubject);
            if (insert != 0){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }

    }
}